package cn.lg.soar.general.entity;

import cn.lg.soar.common.constant.Format;
import cn.lg.soar.common.util.jackson.annotations.SoarFormat;
import cn.lg.soar.core.annotations.constraints.group.Create;
import cn.lg.soar.core.annotations.constraints.group.Update;
import cn.lg.soar.database.base.entity.BaseEntity;
import cn.lg.soar.general.api.enums.FileStorageStatusEnum;
import cn.lg.soar.general.api.enums.FileStorageTypeEnum;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.Version;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;

/**
 * 文件仓库
 * @author luguoxiang
 * @since 2024-03-01
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Data
@ApiModel(value = "FileStorage", description = "文件仓库")
@TableName("lg_general_file_storage")
public class FileStorage extends BaseEntity {

    private static final long serialVersionUID = -709760794007247581L;
    /** id */
    @NotNull(message = "主键不能为空", groups = { Update.class })
    @ExcelIgnore
    @ApiModelProperty(value = "主键")
    @TableId
    private Long id;

    /** 编码 */
    @NotBlank(message = "编码不能为空", groups = { Create.class, Update.class })
    @ExcelProperty(value = "编码")
    @ApiModelProperty(value = "编码")
    private String code;

    /** 名称 */
    @NotBlank(message = "名称不能为空", groups = { Create.class, Update.class })
    @ExcelProperty(value = "名称")
    @ApiModelProperty(value = "名称")
    private String name;

    /** 状态（0正常，1禁用，2只读，3只写） */
    @ExcelProperty(value = "状态")
    @ApiModelProperty(value = "状态（0正常，1禁用，2只读，3只写）")
    private FileStorageStatusEnum status;

    /** 类型（0本地，1OSS） */
    @ExcelProperty(value = "类型")
    @ApiModelProperty(value = "类型（0本地，1OSS）")
    private FileStorageTypeEnum type;

    /** 资源地址，本地：文件路径，oss: url，ftp：host:port/目录 */
    @NotBlank(message = "资源地址不能为空", groups = { Create.class, Update.class })
    @ExcelProperty(value = "资源地址")
    @ApiModelProperty(value = "资源地址")
    private String endpoint;

    /** Access key，本地无效，账号 */
    @SoarFormat(Format.PASSWORD)
    @ExcelProperty(value = "Access key")
    @ApiModelProperty(value = "Access key")
    private String accessKey;

    /** Secret key，本地无效，密码 */
    @SoarFormat(Format.PASSWORD)
    @ExcelProperty(value = "Secret key")
    @ApiModelProperty(value = "Secret key")
    private String secretKey;

    /** 区域，本地无效 */
    @ExcelProperty(value = "区域")
    @ApiModelProperty(value = "区域")
    private String region;

    /** 最大线程数，本地无效 */
    @ExcelProperty(value = "最大线程数")
    @ApiModelProperty(value = "最大线程数")
    private Integer maxConnections;

    /** pathStyle模式，本地无效 */
    @ExcelProperty(value = "pathStyle模式")
    @ApiModelProperty(value = "pathStyle模式")
    private Boolean pathStyleAccess;

    /** 备注 */
    @ExcelProperty(value = "备注")
    @ApiModelProperty(value = "备注")
    private String remark;

    /** 排序 */
    @ExcelProperty(value = "排序")
    @ApiModelProperty(value = "排序")
    private Integer sort;

    /** 默认仓库 */
    @ExcelProperty(value = "默认仓库")
    @ApiModelProperty(value = "默认仓库")
    private Boolean defaulted;

    @Version
    @ApiModelProperty(value = "乐观锁")
    protected Integer version;

    @JsonIgnore
    @Override
    public Serializable primaryKey() {
        return this.id;
    }

    public boolean readable() {
        return FileStorageStatusEnum.normal.equals(status) || FileStorageStatusEnum.read_only.equals(status);
    }

    public boolean writable() {
        return FileStorageStatusEnum.normal.equals(status) || FileStorageStatusEnum.write_only.equals(status);
    }

}
